/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package expression.interval;

/**
 *
 * @author ddoose
 */
public class IntValue extends Interval {
    private int value;
    
    IntValue(int v) {
        value = v;
    }
    
    public boolean isEmpty() {
        return false;
    }
    
    public boolean isUniverse() {
        return false;
    }
    
    public boolean isValue() {
        return true;
    }

    public int getValue() {
        return value;
    }

    public boolean isLeftBounded() {
        return true;
    }
    
    public int getLeftBound() {
        return value;
    }
    
    public boolean isRightBounded() {
        return true;
    }
    
    public int getRightBound() {
        return value;
    }

    public Interval inter(Interval i) {
        if (i instanceof IntValue) {
            if (i.getLeftBound() == getLeftBound()) {
                return this;
            }
            else {
                return new IntEmpty();
            }
        }
        if (isDisjoined(i)) {
            return new IntEmpty();
        }
        return i.inter(this);
    }
    
    public Interval union(Interval i) {
        if (i.isEmpty()) {
            return null;
        }
        else if (i.isStrictLeft(this) && (i.getRightBound() == value - 1)) {
            if (i.isLeftBounded()) {
                return new IntBounded(i.getLeftBound(), value);
            } 
            else {
                return new IntRightBounded(value);
            }
        }
        else if (i.isStrictRight(this) && (i.getLeftBound() == value + 1)) {
            if (i.isRightBounded()) {
                return new IntBounded(value, i.getRightBound());
            } 
            else {
                return new IntLeftBounded(value);
            }
        }
        else {
            return i.clone();
        }
    }
    
    public boolean equals(Interval i) {
        return (i instanceof IntValue) && (value == i.getLeftBound());
    }

    public Interval clone() {
        return new IntValue(value);
    }
    
    public String toString() {
        return "[" + value + "]";
    }
}
